package _thread_lock._volatile;

public class VolatileDemo implements Runnable {

    int i = 0;

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    @Override
    public void run() {
        i++;
        try {
            System.out.println(Thread.currentThread().getId());
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileDemo volatileDemo1 = new VolatileDemo();
        Thread thread1 = new Thread(volatileDemo1);
        thread1.start();
        Thread.sleep(4000);
        volatileDemo1.setI(2);


        new Thread(volatileDemo1).start();
//
//        Thread.sleep(4000);
//        System.out.println(volatileDemo1.i);
    }
}
